<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/logo.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <!-- 页面主体区 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="collapse?'64px':'200px'">
                <el-menu background-color="#333744" text-color="#fff" active-text-color="#ffd04b" unique-opened 
                :collapse="collapse" :collapse-transition="false" router
                :default-active="$route.path">
                    <div class="toggle-button" @click="collapseMenu">|||</div>
                    <!-- 一级菜单模版区 -->
                    <el-submenu :index="menus.id.toString()" v-for="menus in menulist" :key="menus.id">
                        <template slot="title">
                            <!-- 图标 -->
                            <i :class="iconObj[menus.id]"></i>
                            <!-- 文本 -->
                            <span>{{menus.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/' + items.path" v-for="items in menus.children" :key="items.id">
                            <template slot="title">
                                <!-- 图标 -->
                                <i class="el-icon-menu"></i>
                                <!-- 文本 -->
                                <span>{{items.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧边栏 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
    export default {
        name: 'Home',
        
        data() {
            return {
                menulist: [],
                iconObj: {
                    '125': 'iconfont icon-users',
                    '103': 'iconfont icon-tijikongjian',
                    '101': 'iconfont icon-shangpin',
                    '102': 'iconfont icon-danju',
                    '145': 'iconfont icon-baobiao'
                },
                collapse: false,
            };
        },
        created() {
            this.getMenuList()
        },
        mounted() {

        },

        methods: {
            logout() {
                window.sessionStorage.clear()
                this.$router.push('/login')
            },
            async getMenuList() {
                const { data: res } = await this.$http.get('menus')
                if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
                this.menulist = res.data
                console.log(res.data);
            },
            collapseMenu() {
                this.collapse = !this.collapse
            }
        },
    };
</script>

<style scoped>
    .iconfont {
        margin-right: 10px;
    }

    .toggle-button {
        text-align: center;
        cursor: pointer;
        background-color: #4a5064;
        color: #fff;
        letter-spacing: 0.2em;
    }

    .home-container {
        height: 100%;
    }

    .el-header {
        display: flex;
        justify-content: space-between;
        background-color: #373d41;
        padding-left: 0;
        align-items: center;
        color: #fff;
        font-size: 20px;
    }

    .el-header div {
        display: flex;
        align-items: center;
    }

    .el-header div span {
        margin-left: 10px;
    }

    .el-header div img {
        height: 60px;
    }

    .el-aside {
        background-color: #333744;
        transition:0.3s
    }

    .el-menu {
        border-right: 0;
    }

    .el-main {
        background-color: #EAEDF1;
    }
</style>